package com.example.administrator.reader.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.reader.R;
import com.example.administrator.reader.core.BaseActivity;
import com.example.administrator.reader.element.CZhanElement;
import com.example.administrator.reader.element.CZhudaElement;
import com.example.administrator.reader.entity.City;
import com.example.administrator.reader.request.BaseRequest;
import com.example.administrator.reader.request.CityRequest;
import com.example.administrator.reader.utils.Constants;
import com.example.administrator.reader.view.BookView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Created by Administrator on 2017-03-23.
 */
public class MoreBookActivity extends BaseActivity{

    @ViewInject(R.id.fl_more)
    private FrameLayout fl;

//    @ViewInject(R.id.back)
//    private ImageView ivBack;

//    @ViewInject(R.id.tv_title)
//    private TextView tv_title;

    @ViewInject(R.id.tv_type)
    private TextView tv_type;

    @ViewInject(R.id.iv_type)
    private ImageView iv_type;

    private View view;
    private CZhudaElement cZhudaElement;
    private CityRequest request;
    private City cityData;

    private String url = "city.shtml";

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.OK:
                    fillAllElement();
                    break;
            }
        }
    };
    private Intent intentContent;


    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_more_book);
        ViewUtils.inject(this);
    }

    @Override
    protected void setUpView() {

    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            setIntentContent(intent);
        }


        request = new CityRequest();
        request.setInter(new BaseRequest.BaseInter<City>() {
            @Override
            public void handleData(City data) {
                cityData = data;
                Message msg = Message.obtain();
                if (cityData == null) {
                    msg.what = Constants.ERROR;
                } else {
                    msg.what = Constants.OK;
                }
                handler.sendMessage(msg);
            }
        });
        request.firstLoad(url);
        // 装入view
        cZhudaElement = new CZhudaElement(this);
        fl.addView(cZhudaElement.getView());
    }

    // 装入Data
    private void fillAllElement() {
        cZhudaElement.loadData(cityData.getZhanzhengs());
        cZhudaElement.fillView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != BookView.instance)
        {
            BookView.instance.startCloseBookAnimation();
        }
    }
    public void setIntentContent(Intent intentContent) {
        String str = intentContent.getStringExtra("TAG");
        setTitleContent(str);
        setTypeContent(str);
        setImageTypeContent(str);
    }

    public void setImageTypeContent(String str) {
        if (str.equals("热门")) {
            iv_type.setImageResource(R.drawable.detail1);
        } else if (str.equals("精品")) {
            iv_type.setImageResource(R.drawable.detail2);
        } else if (str.equals("推荐")) {
            iv_type.setImageResource(R.drawable.detail3);
        } else if (str.equals("免费")) {
            iv_type.setImageResource(R.drawable.detail3);
        } else if (str.equals("主打")) {
            iv_type.setImageResource(R.drawable.detail3);
        } else if (str.equals("文学")) {
            iv_type.setImageResource(R.drawable.detail3);
        }
    }

    public void setTypeContent(String str) {
        if (str.equals("热门")) {
            tv_type.setText("大家都在看");
        } else if (str.equals("精品")) {
            tv_type.setText("世界文学");
        } else if (str.equals("推荐")) {
            tv_type.setText("责任推荐");
        } else if (str.equals("免费")) {
            tv_type.setText("免费书籍");
        } else if (str.equals("主打")) {
            tv_type.setText("本期主打");
        } else if (str.equals("文学")) {
            tv_type.setText("古典文学");
        }
    }

    /**
     * 设置主题
     * @param str
     */
    public void setTitleContent(String str) {
        if (str.equals("热门")) {
            setUpTitle(R.string.hot);
        } else if (str.equals("精品")) {
            setUpTitle(R.string.jingpin);
        } else if (str.equals("推荐")) {
            setUpTitle(R.string.tuijian);
        } else if (str.equals("免费")) {
            setUpTitle(R.string.free);
        } else if (str.equals("主打")) {
            setUpTitle(R.string.zhuda);
        } else if (str.equals("文学")) {
            setUpTitle(R.string.wenxue);
        }
    }
}
